package com.ruoyi.web.controller.video;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.video.domain.VideoCompany;
import com.ruoyi.video.domain.VideoTemplate;
import com.ruoyi.video.service.IVideoCompanyService;
import com.ruoyi.video.service.IVideoTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.video.domain.VideoRecords;
import com.ruoyi.video.service.IVideoRecordsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生成记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-07
 */
@Controller
@RequestMapping("/video/records")
@Api("记录")
public class VideoRecordsController extends BaseController
{
    private String prefix = "video/records";

    @Autowired
    private IVideoRecordsService videoRecordsService;
    @Autowired
    private IVideoTemplateService templateService;
    @Autowired
    private IVideoCompanyService companyService;

//    @RequiresPermissions("video:records:view")
    @GetMapping()
    public String records()
    {
        return prefix + "/records";
    }

    /**
     * 查询生成记录列表
     */
//    @RequiresPermissions("video:records:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("记录列表分页查询")
    public TableDataInfo list(VideoRecords videoRecords)
    {
        Integer companyId = ShiroUtils.getSysUser().getCompanyId();
        if(companyId != null){
            videoRecords.setCompanyId(companyId);
        }
        startPage();
        List<VideoRecords> list = videoRecordsService.selectVideoRecordsList(videoRecords);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(e->{
                VideoTemplate videoTemplate = templateService.selectVideoTemplateById(e.getTemplateId());
                if(videoTemplate != null){
                    e.setTemplateName(videoTemplate.getTemplateName());
                    if(videoTemplate.getCompanyId() != null){
                        VideoCompany videoCompany = companyService.selectVideoCompanyById(videoTemplate.getCompanyId());
                        if(videoCompany != null){
                            e.setCompanyName(videoCompany.getCompanyName());
                        }
                    }
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出生成记录列表
     */
//    @RequiresPermissions("video:records:export")
    @Log(title = "生成记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoRecords videoRecords)
    {
        List<VideoRecords> list = videoRecordsService.selectVideoRecordsList(videoRecords);
        ExcelUtil<VideoRecords> util = new ExcelUtil<VideoRecords>(VideoRecords.class);
        return util.exportExcel(list, "生成记录数据");
    }

    /**
     * 新增生成记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存生成记录
     */
//    @RequiresPermissions("video:records:add")
    @Log(title = "生成记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VideoRecords videoRecords)
    {
        return toAjax(videoRecordsService.insertVideoRecords(videoRecords));
    }

    /**
     * 修改生成记录
     */
//    @RequiresPermissions("video:records:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        VideoRecords videoRecords = videoRecordsService.selectVideoRecordsById(id);
        mmap.put("videoRecords", videoRecords);
        return prefix + "/edit";
    }

    /**
     * 修改保存生成记录
     */
//    @RequiresPermissions("video:records:edit")
    @Log(title = "生成记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VideoRecords videoRecords)
    {
        return toAjax(videoRecordsService.updateVideoRecords(videoRecords));
    }

    /**
     * 删除生成记录
     */
//    @RequiresPermissions("video:records:remove")
    @Log(title = "生成记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(videoRecordsService.deleteVideoRecordsByIds(ids));
    }
}
